import { treeselect } from "@/api/system/dept.js";
const checkPhone = (rule, value, callback) => {
  if (!/^1[3-9]\d{9}$/.test(value)) {
    return callback(new Error("请输入正确的手机号码"));
  }
  callback();
};
const checkMobile = (rule, value, callback) => {
  const mobileReg = /^1[3-9]\d{9}$/;
  const landlineReg = /^(0\d{2,3})-?(\d{7,8})$/;
  if (value) {
    if (!mobileReg.test(value) && !landlineReg.test(value)) {
      return callback(new Error("请输入正确的号码"));
    }
  }
  callback();
};
export default {
  data() {
    return {
      treeData: [],
    };
  },
  computed: {
    baseFileds() {
      return [
        // {
        //   key: "userType",
        //   filedType: "Select",
        //   label: "用户类型",
        //   span: 24,
        //   props: {
        //     rules: [
        //       { required: true, message: "请选择用户类型", trigger: "change" },
        //     ],
        //     options: [
        //       {
        //         label: "员工",
        //         value: 0,
        //       },
        //       {
        //         label: "用户",
        //         value: 1,
        //       },
        //     ],
        //   },
        // },
        {
          key: "name",
          filedType: "Input",
          label: "姓名",
          span: 12,
          props: {
            disabled: this.type === "detail",
            rules: [{ required: true, message: "请输入姓名", trigger: "blur" }],
          },
        },
        {
          key: "account",
          filedType: "Input",
          label: "账号",
          span: 12,
          props: {
            disabled: this.type === "detail",
            rules: [{ required: true, message: "请输入账号", trigger: "blur" }],
          },
        },
        {
          key: "password",
          filedType: "Input",
          label: "密码",
          span: 12,
          props: {
            disabled: true,
            rules: [{ required: true, message: "请输入密码", trigger: "blur" }],
          },
        },
        {
          key: "password1",
          filedType: "Input",
          label: "确认密码",
          span: 12,
          props: {
            disabled: true,
            rules: [
              { required: true, message: "请输入确认密码", trigger: "blur" },
            ],
          },
        },
        {
          key: "mobile",
          filedType: "Input",
          label: "手机号码",
          span: 12,
          props: {
            disabled: this.type === "detail",
            placeholder: "请输入手机号码",
            rules: [
              { required: true, message: "请输入手机号码", trigger: "blur" },
              { validator: checkPhone, trigger: "blur" },
            ],
          },
        },
        {
          key: "email",
          filedType: "Input",
          label: "邮箱地址",
          span: 12,
          props: {
            disabled: this.type === "detail",
            placeholder: "请输入邮箱地址",
            rules: [
              {
                type: "email",
                message: "请输入正确的邮箱地址",
                trigger: ["blur"],
              },
            ],
          },
        },
        {
          key: "status",
          filedType: "Radio",
          label: "状态",
          span: 12,
          props: {
            disabled: this.type === "detail",
            options: [
              { value: 0, label: "无效" },
              { value: 1, label: "有效" },
              { value: 8, label: "冻结" },
            ],
            rules: [
              { required: true, message: "请选择状态", trigger: "change" },
            ],
          },
        },
        {
          key: "userType",
          filedType: "Radio",
          label: "是否注册",
          span: 12,
          props: {
            disabled: this.type === "detail",
            options: [
              { value: 0, label: "未注册" },
              { value: 1, label: "已注册" },
            ],
            rules: [
              { required: true, message: "请选择状态", trigger: "change" },
            ],
          },
        },
      ];
    },
    postFileds() {
      return [
        {
          key: "deptId",
          filedType: "Cascader",
          label: "工作单位",
          span: 24,
          props: {
            options: this.treeData,
            filterable: true,
            showAllLevels: false,
            disabled: this.type === "detail",
            props: {
              multiple: false,
              value: "id",
              label: "label",
              children: "children",
              showAllLevels: false,
              checkStrictly: false,
            },
            rules: [
              { required: true, message: "请选择工作单位", trigger: "change" },
            ],
          },
        },
        {
          key: "positionType",
          filedType: "Radio",
          label: "职务类型",
          span: 24,
          props: {
            disabled: this.type === "detail",
            options: [
              { value: "1", label: "负责人" },
              { value: "2", label: "副负责人" },
              { value: "0", label: "普通人员" },
            ],
            rules: [
              { required: true, message: "请选择职务类型", trigger: "change" },
            ],
          },
        },
        {
          key: "positionName",
          filedType: "Input",
          label: "职务名称",
          span: 12,
          props: {
            disabled: this.type === "detail",
            rules: [
              { required: true, message: "请输入职务名称", trigger: "blur" },
            ],
          },
        },
        {
          key: "positionOrderNo",
          filedType: "Input",
          label: "排序编码",
          span: 12,
          props: {
            disabled: this.type === "detail",
            type: "number",
            rules: [
              { required: true, message: "请输入排序编码", trigger: "blur" },
            ],
          },
        },
      ];
    },
    otherFileds() {
      return [
        {
          key: "code",
          filedType: "Input",
          label: "编码",
          span: 12,
          props: {
            disabled: this.type === "detail",
          },
        },
        {
          key: "workDate",
          filedType: "DatePicker",
          label: "参加工作时间",
          span: 12,
          props: {
            disabled: this.type === "detail",
          },
        },
        {
          key: "birthday",
          filedType: "DatePicker",
          label: "出生日期",
          span: 12,
          props: {
            disabled: this.type === "detail",
          },
        },
        {
          key: "sex",
          filedType: "Radio",
          label: "性别",
          span: 12,
          props: {
            disabled: this.type === "detail",
            options: [
              { value: 1, label: "男" },
              { value: 0, label: "女" },
              { value: 2, label: "其他" },
            ],
          },
        },
        {
          key: "workTel",
          filedType: "Input",
          label: "工作电话",
          span: 12,
          props: {
            disabled: this.type === "detail",
            rules: [{ validator: checkMobile, trigger: "blur" }],
          },
        },
        {
          key: "homeTel",
          filedType: "Input",
          label: "家庭电话",
          span: 12,
          props: {
            disabled: this.type === "detail",
          },
        },
        {
          key: "linkman",
          filedType: "Input",
          label: "紧急联系人",
          span: 12,
          props: {
            disabled: this.type === "detail",
            rules: [{ validator: checkMobile, trigger: "blur" }],
          },
        },
        {
          key: "linkmanTel",
          filedType: "Input",
          label: "紧急联系人电话",
          span: 12,
          props: {
            disabled: this.type === "detail",
            rules: [{ validator: checkMobile, trigger: "blur" }],
          },
        },
        {
          key: "homeAddres",
          filedType: "Input",
          label: "家庭住址",
          span: 24,
          props: {
            disabled: this.type === "detail",
          },
        },
      ];
    },
  },
  created() {
    this.getTree();
  },
  methods: {
    async getTree() {
      const { code, data } = await treeselect();
      if (code === 200) {
        this.treeData = data;
      }
    },
  },
};
